<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="充值名称" prop="shopRechargeName">
        <el-input v-model="queryParams.shopRechargeName" placeholder="请输入商品充值名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单金额" prop="orderPrice">
        <el-input v-model="queryParams.orderPrice" placeholder="请输入订单金额" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable v-model="queryParams.payTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:clientOrder:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:clientOrder:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:clientOrder:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:clientOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="clientOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="手机号" align="center" prop="username" />
      <el-table-column label="商品充值名称" align="center" prop="shopRechargeName" />
      <el-table-column label="商品" align="center" prop="coins" >
        <template #default="scope">
          <span>{{ scope.row.coins }}金币</span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" align="center" prop="orderPrice" />
      <el-table-column label="支付方式" align="center" prop="payType">
        <template #default="scope">
          <span>{{ scope.row.payType === 1 ? '微信' : '支付宝' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payType">
        <template #default="scope">
          <span>{{ scope.row.payStatus === 1 ? '未支付' : scope.row.payStatus === 2 ? '支付成功' : scope.row.payStatus === 4 ?
            '已取消' : scope.row.payStatus === 5 ? '已退款' : ''}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:clientOrder:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:clientOrder:remove']">删除</el-button>
          </template>
      </el-table-column> -->
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改客户端订单对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="clientOrderRef" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ClientOrder">
import { listClientOrder, getClientOrder, delClientOrder, addClientOrder, updateClientOrder } from "@/api/business/clientOrder";

const { proxy } = getCurrentInstance();

const clientOrderList = ref([]);
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shopRechargeName: undefined,
    orderPrice: undefined,
    coins: undefined,
    payType: undefined,
    payStatus: undefined,
    payTime: undefined,
    orderNo: undefined
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    clientUserId: [
      { required: true, message: "客户端用户ID不能为空", trigger: "blur" }
    ],
    shopRechargeId: [
      { required: true, message: "商品充值ID不能为空", trigger: "blur" }
    ],
    shopRechargeName: [
      { required: true, message: "商品充值名称不能为空", trigger: "blur" }
    ],
    orderPrice: [
      { required: true, message: "订单金额不能为空", trigger: "blur" }
    ],
    coins: [
      { required: true, message: "金币数量不能为空", trigger: "blur" }
    ],
    payType: [
      { required: true, message: "支付方式 1 微信 2 支付宝。。。不能为空", trigger: "change" }
    ],
    payStatus: [
      { required: true, message: "支付状态， 1 未支付， 2 支付成功， 4 已取消， 5 已退款不能为空", trigger: "change" }
    ],
    payTime: [
      { required: true, message: "支付时间不能为空", trigger: "blur" }
    ],
    orderNo: [
      { required: true, message: "订单编号不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询客户端订单列表 */
function getList() {
  loading.value = true;
  listClientOrder(queryParams.value).then(response => {
    clientOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    clientUserId: null,
    shopRechargeId: null,
    shopRechargeName: null,
    orderPrice: null,
    coins: null,
    payType: null,
    payStatus: null,
    createTime: null,
    payTime: null,
    orderNo: null
  };
  proxy.resetForm("clientOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加客户端订单";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getClientOrder(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改客户端订单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["clientOrderRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateClientOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addClientOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除客户端订单编号为"' + _ids + '"的数据项？').then(function () {
    loading.value = true;
    return delClientOrder(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/clientOrder/export', {
    ...queryParams.value
  }, `clientOrder_${new Date().getTime()}.xlsx`)
}

getList();
</script>
